package com.zhong.scanerdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhong.scanerdemo.interfaces.IHttpRequest;
import com.zhong.scanerdemo.model.UploadResult;
import com.zhong.scanerdemo.network.RequestRunnable;
import com.zhong.scanerdemo.utils.ClientLogUtil;
import com.zhong.scanerdemo.utils.GsonUtil;
import com.zhong.scanerdemo.utils.KeyAndUrl;
import com.zhong.scanerdemo.utils.ScanerParameters;

public class ResultActivity extends Activity implements IHttpRequest {
	private final String TAG = "ResultActivity";
	private SharedPreferences sharedPreferences;
	private ProgressDialog pDialog = null;
	private TextView tv_result;
	private Button btn_upload, btn_back;
	private String scanResult, dutyOfficer, driver, carNum, startingPoint,
			destination, uid, pwd, source, startDate, method, descr;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			pDialog.dismiss();
			if (msg.getData().getString("state").equals("1")) {
				Toast.makeText(getApplicationContext(), "上传成功", 1000).show();
				ResultActivity.this.finish();
			} else {
				android.app.AlertDialog.Builder builder = new AlertDialog.Builder(
						ResultActivity.this);
				builder.setTitle("结果");
				builder.setMessage(msg.getData().getString("msg"));
				builder.setCancelable(false);
				builder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								ResultActivity.this.finish();
							}
						});
				builder.create().show();
			}
			
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_result);

		sharedPreferences = this.getSharedPreferences("scaner",
				MODE_WORLD_READABLE);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		scanResult = bundle.getString("result");

		tv_result = (TextView) findViewById(R.id.tv_result);
		btn_upload = (Button) findViewById(R.id.btn_upload);
		btn_back = (Button) findViewById(R.id.btn_back);

		ininViews();

	}

	private void ininViews() {

		dutyOfficer = sharedPreferences.getString("dutyOfficer", "");
		driver = sharedPreferences.getString("driver", "");
		carNum = sharedPreferences.getString("carNum", "");
		startingPoint = sharedPreferences.getString("startingPoint", "");
		destination = sharedPreferences.getString("destination", "");
		// 新增
		uid = sharedPreferences.getString("uid", "");
		pwd = sharedPreferences.getString("pwd", "");
		source = sharedPreferences.getString("source", "");
		startDate = sharedPreferences.getString("startDate", "");
		method = sharedPreferences.getString("method", "");
		descr = sharedPreferences.getString("descr", "");

		tv_result.setText("\n" + "扫描结果" + "\n" + scanResult + "\n\n" + "相关信息"
				+ "\n" + "账号名：" + uid + "\n" + "产品来源：" + source + "\n" + "责任人："
				+ dutyOfficer + "\n" + "运输者：" + driver + "\n" + "运输日期："
				+ startDate + "\n" + "运输方式：" + method + "\n" + "车牌/航班/列次："
				+ carNum + "\n" + "开始地址：" + startingPoint + "\n" + "结束地址："
				+ destination + "\n" + "备注：" + descr);

		btn_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ResultActivity.this.finish();
			}
		});

		btn_upload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				RequestRunnable runnable = new RequestRunnable(ScanerParameters
						.initParameter(uid, pwd, source, startDate,
								method, descr, dutyOfficer,
								driver, carNum, startingPoint, destination,
								scanResult).getNameValuePairs(), ResultActivity.this,
						KeyAndUrl.REQUEST_URL);

				Thread thread = new Thread(runnable);
				pDialog = new ProgressDialog(ResultActivity.this);
				pDialog.setMessage("正在 上传，请稍候...");
				pDialog.setCancelable(false);
				pDialog.show();
				thread.start();

			}
		});

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		ResultActivity.this.finish();
	}

	@Override
	public void refresh(String result) {
		// TODO Auto-generated method stub
		ClientLogUtil.i(TAG, "result----------->" + result);
		Message msg = handler.obtainMessage();
		Bundle bundle = new Bundle();
		UploadResult uploadResult=GsonUtil.useGson(result);
		if(uploadResult!=null){
			bundle.putString("state", uploadResult.getState());
			bundle.putString("msg", uploadResult.getMsg());
			ClientLogUtil.i(TAG,
					"josn result----------->" + uploadResult.toString());
		}else {
			bundle.putString("state", "");
			bundle.putString("msg", "网络出错");
		}
		msg.setData(bundle);
		handler.sendMessage(msg);
	}

}
